<template>
  <div class="app-container">
    <div style="padding:20px 100px 10px 100px;">
      <el-upload :data="dataObj" :headers="headers" :on-success="onSuccess" :action="uploadUrl" drag>
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
      </el-upload>
      <h4>文件地址</h4>
      <el-input v-model="text" type="textarea" />
    </div>
    <div class="filter-container">
      <el-input v-model="listQuery.fileName" placeholder="File Nam" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.location" placeholder="Location" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.fileName" class="filter-item" @change="tableKey = tableKey + 1">File Name</el-checkbox>
        <el-checkbox v-model="showRows.fileUrl" class="filter-item" @change="tableKey = tableKey + 1">File Url</el-checkbox>
        <el-checkbox v-model="showRows.category" class="filter-item" @change="tableKey = tableKey + 1">Category</el-checkbox>
        <el-checkbox v-model="showRows.location" class="filter-item" @change="tableKey = tableKey + 1">Location</el-checkbox>
        <el-checkbox v-model="showRows.origin" class="filter-item" @change="tableKey = tableKey + 1">origin</el-checkbox>
        <el-checkbox v-model="showRows.description" class="filter-item" @change="tableKey = tableKey + 1">Description</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>

      <!-- <el-button v-waves class="filter-item" type="primary" plain icon="el-icon-plus" @click="handleCreate" /> -->
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="ID" width="80" prop="id" />
      <el-table-column v-if="showRows.fileName" header-align="center" label="File Name" prop="fileName" show-overflow-tooltip />
      <el-table-column v-if="showRows.fileUrl" header-align="center" label="File Url" prop="fileUrl" show-overflow-tooltip />
      <el-table-column v-if="showRows.category" align="center" label="Category" prop="category" show-overflow-tooltip />
      <el-table-column v-if="showRows.location" header-align="center" label="location" prop="location" show-overflow-tooltip />
      <el-table-column v-if="showRows.origin" header-align="center" label="origin" prop="origin" show-overflow-tooltip />
      <el-table-column v-if="showRows.description" header-align="center" label="description" prop="description" show-overflow-tooltip />

      <el-table-column width="180px" align="center" prop="create_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="Actions" width="130">
        <template slot-scope="scope">
          <el-button v-clipboard:copy="scope.row.fileUrl" v-clipboard:success="clipboardSuccess" type="primary" icon="el-icon-document-copy" plain />
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="handleDelete(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { fetchList, deleteUploadFile } from '@/api/info/upload-file-record'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { mapGetters } from 'vuex'
import clipboard from '@/directive/clipboard/index.js' // use clipboard by v-directive

export default {
  components: { Pagination },
  directives: { waves, clipboard },
  data() {
    return {
      headers: { Authorization: getToken() || '' },
      dataObj: { shard: 'cms' },
      uploadUrl: process.env.VUE_APP_BASE_API + '/sys/common/qiniu/upload',
      text: '',
      tableKey: 0,
      showRows: {
        fileName: true,
        fileUrl: true,
        category: true,
        origin: true,
        location: true,
        description: true
      },
      list: null,
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        uid: undefined,
        fileName: undefined,
        category: undefined,
        location: undefined,
        current: 1,
        size: 10,
        asc: undefined,
        desc: ['create_at']
      }
    }
  },
  computed: {
    ...mapGetters(['uid'])
  },
  created() {
    console.log('ts')
    this.listQuery.uid = this.uid
    this.getList()
  },
  methods: {
    onSuccess(response, file) {
      this.text = response.result
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery)
        .then(response => {
          this.list = response.result.list
          this.total = response.result.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    },
    clipboardSuccess() {
      this.$message({
        message: 'Copy successfully',
        type: 'success',
        duration: 1500
      })
    },
    handleDelete(id) {
      this.$confirm('Confirm delete record?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        deleteUploadFile(id).then(response => {
          console.log(response)
          this.$message({
            type: 'success',
            message: 'deleted successfully!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.el-upload--text {
  width: 500px;
  height: 220px;
  .el-upload-dragger {
    width: 100%;
    height: 220px;
  }
}
</style>
